{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 多维标度法(MDS)\n",
    "如,给定城市间距，要求画出地理地图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "特征值:  [-4.44089210e-16  3.00000000e+00  0.00000000e+00  3.00000000e+00\n",
      " -3.35091195e-16  2.67152249e-16 -4.23403877e-17]\n",
      "特征向量:  [[-8.16496581e-01  5.77350269e-01  3.27700260e-01 -3.84498439e-02\n",
      "  -5.99652411e-01 -6.64019537e-02 -4.19219282e-01]\n",
      " [ 2.04124145e-01  2.88675135e-01 -3.20261820e-01 -5.18114897e-01\n",
      "   2.93065389e-01  3.97265441e-01  1.62109140e-01]\n",
      " [-2.04124145e-01 -2.88675135e-01  6.20053905e-01 -4.79665053e-01\n",
      "  -6.82424708e-01 -3.23687554e-01 -6.15575006e-01]\n",
      " [-4.08248290e-01 -5.77350269e-01 -3.93437111e-01  3.84498439e-02\n",
      "  -3.76624831e-02  4.62077874e-01  3.00893904e-01]\n",
      " [-2.04124145e-01 -2.88675135e-01  4.00875551e-01  5.18114897e-01\n",
      "  -2.68924539e-01 -1.31214387e-01 -5.58004045e-01]\n",
      " [ 2.04124145e-01  2.88675135e-01 -1.01083466e-01  4.79665053e-01\n",
      "  -1.20434780e-01  2.04792274e-01  1.04538179e-01]\n",
      " [ 3.39934989e-17  4.80740672e-17  2.82606134e-01  4.32100625e-17\n",
      "   2.41407165e-02  6.78472008e-01 -7.87993990e-02]]\n",
      "变换到二维坐标:  [[-0.23528568 -0.4841652 ]\n",
      " [ 0.42278485  0.29082297]\n",
      " [ 1.58835779 -0.08619089]\n",
      " [ 1.73124493 -1.14635992]\n",
      " [ 0.4174536  -1.74183876]\n",
      " [-0.37862629 -1.86529401]\n",
      " [ 0.34475339 -0.91617044]]\n"
     ]
    }
   ],
   "source": [
    "# 课本例10.20实现\n",
    "import numpy as np\n",
    "\n",
    "D = np.array(\n",
    "    [[0, 1, np.sqrt(3), 2, np.sqrt(3), 1, 1],\n",
    "     [0, 0, 1, np.sqrt(3), 2, np.sqrt(3), 1],\n",
    "     [0, 0, 0, 1, np.sqrt(3), 2, 1],\n",
    "     [0, 0, 0, 0, 1, np.sqrt(3), 1],\n",
    "     [0, 0, 0, 0, 0, 1, 1],\n",
    "     [0, 0, 0, 0, 0, 0, 1],\n",
    "     [0, 0, 0, 0, 0, 0, 0]],\n",
    "    dtype=np.float)\n",
    "\n",
    "D = D + D.T\n",
    "\n",
    "A = -D*D/2\n",
    "H = np.eye(A.shape[0]) - np.ones(A.shape)/A.shape[0]\n",
    "B = np.matmul(H, np.matmul(A, H))\n",
    "eigenvalues, eigenvectors = np.linalg.eig(B)\n",
    "print('特征值: ', eigenvalues)\n",
    "print('特征向量: ', eigenvectors)\n",
    "# 取最大两个\n",
    "trans = eigenvectors[[1,3]]\n",
    "coordinate = np.matmul(D, trans.T)\n",
    "print('变换到二维坐标: ', coordinate)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
